﻿using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Serilog;
using System;
using System.IO;

namespace AuthServer.Host
{
    public class Program
    {
        public static int Main(string[] args)
        {
            //TODO: Temporary: it's not good to read appsettings.json here just to configure logging
            var currentEnv = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
            Console.WriteLine($"当前环境--{currentEnv}--");
            var configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json")
                .AddJsonFile($"appsettings.{currentEnv}.json", optional: true)
                .AddEnvironmentVariables()
                .Build();

            //Log.Logger = new LoggerConfiguration()
            //    .MinimumLevel.Warning()
            //    .MinimumLevel.Override("Volo", LogEventLevel.Warning)
            //    .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
            //    .MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Warning)
            //    .MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Warning)
            //    .MinimumLevel.Override("System", LogEventLevel.Warning)
            //    .MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Warning)
            //    .Enrich.WithProperty("Application", "AuthServer")
            //    .Enrich.FromLogContext()
            //    //.WriteTo.File("Logs/logs.txt")
            //    .WriteTo.Console()
            //                    //.WriteTo.Elasticsearch(
            //                    //    new ElasticsearchSinkOptions(new Uri(configuration["ElasticSearch:Url"]))
            //                    //    {
            //                    //        AutoRegisterTemplate = true,
            //                    //        AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv6,
            //                    //        IndexFormat = $"AuthServer.Host.{currentEnv}-{0:yyyy.MM}"
            //                    //    })
            //    .WriteTo.Elasticsearch(new[] { new Uri(configuration["ElasticSearch:Url"]) }, opts =>
            //    {
            //        opts.DataStream = new DataStreamName($"AuthServer.Host", currentEnv, DateTime.Now.ToString("yyyy-MM-dd"));
            //        opts.BootstrapMethod = BootstrapMethod.Failure;

            //        opts.TextFormatting = new EcsTextFormatterConfiguration
            //        {
            //            MapCustom = (e, _) => e
            //        };
            //        opts.ConfigureChannel = channelOpts =>
            //        {
            //            channelOpts.BufferOptions = new BufferOptions
            //            {
            //                ExportMaxConcurrency = 10
            //            };
            //        };
            //    })
            //    .CreateLogger();

            try
            {
                Console.WriteLine($"Starting AuthServer.Host.--ASPNETCORE_ENVIRONMENT--{currentEnv}");
                CreateHostBuilder(args).Build().Run();
                return 0;
            }
            catch (Exception ex)
            {
                Log.Fatal(ex, ex.Message);
                return 1;
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }

        internal static IHostBuilder CreateHostBuilder(string[] args) =>
            Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                })
                .UseAutofac()
                .UseSerilog();
    }
}
